/**
 * 
 */
package com.bankcomm.ui.specialwidget;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.bankcomm.R;
import com.bankcomm.bean.Content;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BaseViewCopy;
import com.bankcomm.ui.adapter.NameAdapter;
import com.bankcomm.util.PinyinUtils;
import com.rytong.tools.ui.Component;

/**
 * @author James
 * 
 */
public class LPLinkManLayout extends Component {
	public NameAdapter adapter;

	@Override
	public void initRealView(Activity activity, String nodeValue) {
		super.initRealView(activity, nodeValue);
		realView_ = new MyLinkManLayout(activity);

	}

	public class MyLinkManLayout extends LinearLayout implements
			CompositedComponent {

		private ListView mListView;
		private com.bankcomm.ui.specialwidget.SideBar indexBar;
		private ArrayList<Content> list;
		private String subUrl;

		public MyLinkManLayout(Context context) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.bcm_linkman_layout,
					this);
			mListView = (ListView) this.findViewById(R.id.list);
			mListView.setSelector(R.drawable.link_list_select_bg);
			indexBar = (com.bankcomm.ui.specialwidget.SideBar) findViewById(R.id.sideBar);
			list = new ArrayList<Content>();
			String data = getPropertyByName("data");
			if (!data.equals("")) {
				handlerJsonData(data, list);
			}
			// 根据a-z进行排序
			Collections.sort(list, new PinyinComparator());
			// 实例化自定义内容适配类
			adapter = new NameAdapter(context, list);
			// 为listView设置适配
			mListView.setAdapter(adapter);
			subUrl = getPropertyByName("url");
			mListView.setOnItemClickListener(mItemClickListener);
			indexBar.setListView(mListView);
		}

		OnItemClickListener mItemClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (!subUrl.equals("")) {
					if(!subUrl.startsWith("/")){
						subUrl="/".concat(subUrl);
					}
					// f发送请求
					String subParm = list.get(position).getParms();
					LPMid.getInstance().um_.sendHttpRequest(
							(BaseViewCopy) getContext(), ConfigManager.SERVER_URI+subUrl, subParm);
				}
			}
		};

		private void handlerJsonData(String data, List<Content> list) {
			try {
				JSONArray json = new JSONArray(data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject obj = (JSONObject) json.get(i);
					Content content = new Content();
					String name = obj.getString("name");
					content.setName(name);
					content.setTel(obj.getString("tel"));
					content.setCardtype(obj.getString("type"));
					content.setCardnum(obj.getString("num"));
					content.setParms(obj.getString("parm"));
					content.setLetter(PinyinUtils.getFirtChar(name));
					list.add(content);
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}

		}

		@Override
		public Component composited() {

			return LPLinkManLayout.this;
		}

		@Override
		public boolean onInterceptTouchEvent(MotionEvent ev) {
			ViewGroup vf = (ViewGroup) getParent();
			ViewGroup vg = (ViewGroup) (vf.getParent());
			if (vg == null) {
				return super.onInterceptTouchEvent(ev);
			} else {
				if (ev.getAction() == MotionEvent.ACTION_DOWN) {
					// 将父scrollview的滚动事件拦截
					vg.requestDisallowInterceptTouchEvent(true);
				} else if (ev.getAction() == MotionEvent.ACTION_UP) {
					// 把滚动事件恢复给父Scrollview
					vg.requestDisallowInterceptTouchEvent(false);
				}
			}
			return super.onInterceptTouchEvent(ev);
		}

	}

	@Override
	public void mouldH() throws Exception {
		super.mouldH();
	}

	public class PinyinComparator implements Comparator<Content> {

		public int compare(Content o1, Content o2) {
			if (o1.getLetter().equals("@") || o2.getLetter().equals("#")) {
				return -1;
			} else if (o1.getLetter().equals("#") || o2.getLetter().equals("@")) {
				return 1;
			} else {
				return o1.getLetter().compareTo(o2.getLetter());
			}
		}

	}
}
